package cn.dujc.superkid.fragment.MyCourserFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import cn.dujc.core.adapter.BaseQuickAdapter;
import cn.dujc.core.ui.BaseFragment;
import cn.dujc.core.ui.BaseListFragment;
import cn.dujc.core.util.ToastUtil;
import cn.dujc.superkid.R;
import cn.dujc.superkid.activities.CourseDetailActivity;
import cn.dujc.superkid.activities.LessonDetailActivity;
import cn.dujc.superkid.adapter.CourseAdapter;
import cn.dujc.superkid.adapter.MyCourseAdapter;
import cn.dujc.superkid.api.HttpHelper;
import cn.dujc.superkid.api.QMap;
import cn.dujc.superkid.api.Result;
import cn.dujc.superkid.api.ResultCallBack;
import cn.dujc.superkid.bean.ChannelEntity;
import cn.dujc.superkid.bean.CourseResult;
import cn.dujc.superkid.bean.MyClassResult;
import cn.dujc.superkid.bean.RegisterResult;
import cn.dujc.superkid.utils.Preferences;
import retrofit2.Call;


public class KnowledgeFragment extends BaseListFragment implements android.view.View.OnClickListener {
    private final List<MyClassResult.MyClass> mList = new ArrayList<>();
    private MyCourseAdapter myCourseAdapter;
    private int mPage = 1;
    private RegisterResult loginResult;
    private final String  TAG  = KnowledgeFragment.class.getName();
    @Override
    public void onClick(android.view.View view) {

    }

    @Nullable
    @Override
    protected BaseQuickAdapter initAdapter() {
        return myCourseAdapter = new MyCourseAdapter(mList);
    }

    @Override
    protected void onItemClick(int position) {
//        Intent in = new Intent();
//        in.setClass(mActivity, LessonDetailActivity.class);
//        in.putExtra("class_title", mList.get(position).getClass_title());
//        in.putExtra("class_id", mList.get(position).getClass_id());
//        mActivity.startActivity(in);
        starter()
                .with(LessonDetailActivity.LESSON_COURSE, myCourseAdapter.getItem(position))
                .go(LessonDetailActivity.class);
    }

    @Override
    protected void loadMore() {
        loadData();
    }

    @Override
    protected void reload() {
        showRefreshing();
        mPage = 0;
        loadData();
    }
    private void loadData() {
        Log.i(TAG,"==============caizhijia2======KnowledgeFragment==loaddata===" );
        loginResult = Preferences.getInstance().getLoginResult(mActivity);
        HttpHelper.getUserCenter().memberMyClass(QMap.build()
                .put("user_id", Integer.parseInt(loginResult.getUid()))
                .put("page",mPage)
                .put("num",7 )
                .put("type_id",1 )
                .put("channel",  Preferences.getInstance().getChannel(mActivity).getChannel_id())
                .put("logincode",loginResult.getLogincode() ).create()).enqueue(new ResultCallBack<Result<MyClassResult>>(mActivity) {
            @Override
            public void onSuccessCallback(int code, Result<MyClassResult> body) {
                if (mPage==0){
                    mPage++;
                }
                if (mPage <= 1) mList.clear();
                boolean done = false;
                if (body != null) {
                    final MyClassResult data = body.getData();
                    if (data != null) {
                        final List<MyClassResult.MyClass> list = data.getList();

                        if (list != null) {
                            mList.addAll(list);
                            Log.i(TAG,"==============caizhijia2======KnowledgeFragment=====" +list.size());
                        }
                        done = mPage >= data.getPage_num();
                    }
                }
                notifyDataSetChanged(done);
                mPage++;
            }

            @Override
            public void onFailureCallback(int code, String msg) {
                super.onFailureCallback(code, msg);
                if(code==400){
                    myCourseAdapter.setEmptyView(R.layout.layout_empty_img);
                }
                loadFailure();
            }
        });
    }
}
